# create a new mailman server
# NOTE: make sure there is room/space for this server on the vmhost
# NOTE: most of these vars_path come from group_vars/mirrorlist or from hostvars

- name: make mailman server
  hosts: mailman-stg:mailman
  user: root
  gather_facts: False
  accelerate: "{{ accelerated }}"

  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - "{{ private }}/vars.yml"
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  tasks:
  - include: "{{ tasks }}/virt_instance_create.yml"
  - include: "{{ tasks }}/accelerate_prep.yml"

  handlers:
  - include: "{{ handlers }}/restart_services.yml"

- name: make the box be real
  hosts: mailman-stg:mailman
  user: root
  gather_facts: True
  accelerate: "{{ accelerated }}"

  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - "{{ private }}/vars.yml"
  - "/srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml"

  roles:
  - base
  - rkhunter
  - { role: denyhosts, when: ansible_distribution_major_version != '7' }
  - nagios_client
  - hosts
  - fas_client
  - collectd/base
  - yum-cron
  - sudo

  tasks:
  # this is how you include other task lists
  - include: "{{ tasks }}/yumrepos.yml"
  - include: "{{ tasks }}/2fa_client.yml"
  - include: "{{ tasks }}/motd.yml"
  - include: "{{ tasks }}/apache.yml"
  - include: "{{ tasks }}/mod_wsgi.yml"
  - include: "{{ tasks }}/openvpn_client_7.yml"
    when: env != "staging"

  handlers:
  - include: "{{ handlers }}/restart_services.yml"


#
# Database setup
#

- name: prepare setting up the database
  hosts: db02.stg.phx2.fedoraproject.org:db01.phx2.fedoraproject.org
  gather_facts: no
  user: root

  tasks:
  - name: install psycopg2 for the postgresql ansible modules
    yum: pkg=python-psycopg2 state=installed
    tags:
    - packages

- name: setup the database
  hosts: db02.stg.phx2.fedoraproject.org:db01.phx2.fedoraproject.org
  gather_facts: no
  sudo: yes
  sudo_user: postgres
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - "{{ private }}/vars.yml"
  - "/srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml"

  tasks:
  # mailman auto-updates its schema, there can only be one admin user
  - name: mailman DB user
    postgresql_user: name=mailmanadmin password={{ mailman_mm_db_pass }}
  - name: hyperkitty DB admin user
    postgresql_user: name=hyperkittyadmin password={{ mailman_hk_admin_db_pass }}
  - name: hyperkitty DB user
    postgresql_user: name=hyperkittyapp password={{ mailman_hk_db_pass }}
  - name: kittystore DB admin user
    postgresql_user: name=kittystoreadmin password={{ mailman_ks_admin_db_pass }}
  - name: kittystore DB user
    postgresql_user: name=kittystoreapp password={{ mailman_ks_db_pass }}
  - name: databases creation
    postgresql_db: name={{ item }} owner="{{ item }}admin" encoding=UTF-8
    with_items:
    - mailman
    - hyperkitty
    - kittystore
  - name: test database creation
    postgresql_db: name=test_hyperkitty owner=hyperkittyadmin encoding=UTF-8


# Real MM/HK-specific work
- name: setup mailman and hyperkitty
  hosts: mailman-stg:mailman
  user: root
  gather_facts: True

  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - "{{ private }}/vars.yml"
  - "/srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml"

  roles:
  - role: mailman
    mailman_mm_db_pass: "{{ mailman_mm_db_pass }}"
    mailman_hk_admin_db_pass: "{{ mailman_hk_admin_db_pass }}"
    mailman_hk_db_pass: "{{ mailman_hk_db_pass }}"
    mailman_ks_admin_db_pass: "{{ mailman_ks_admin_db_pass }}"
    mailman_ks_db_pass: "{{ mailman_ks_db_pass }}"
  - fedmsg/base

  tasks:
  - name: install more needed packages
    yum: pkg={{ item }} state=installed
    with_items:
    - tar
    - mailman # transition from mailman2.1
    tags:
    - packages

  #- name: easy access to the postgresql databases
  #  template: src=$files/mailman/pgpass.j2 dest=/root/.pgpass
  #            owner=root group=root mode=0600

  - name: start services
    service: state=started enabled=yes name={{ item }}
    with_items:
    - httpd
    - mailman3
    - postfix

  handlers:
  - include: "{{ handlers }}/restart_services.yml"
